{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fc0929f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Loading checkpoint shards: 100%|██████████| 4/4 [00:08<00:00,  2.05s/it]\n",
      "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
     ]
    }
   ],
   "source": [
    "from transformers import AutoModelForCausalLM, AutoTokenizer\n",
    "device = \"cuda:0\" # the device to load the model onto\n",
    "\n",
    "model = AutoModelForCausalLM.from_pretrained(\"/gemini/platform/public/llm/huggingface/Qwen/Qwen1.5-7B-Chat\").to(device)\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"/gemini/platform/public/llm/huggingface/Qwen/Qwen1.5-7B-Chat\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cfd35ead",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.10/dist-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "Loading checkpoint shards: 100%|██████████| 2/2 [00:08<00:00,  4.47s/it]\n",
      "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
     ]
    }
   ],
   "source": [
    "from transformers import AutoModelForCausalLM, AutoTokenizer\n",
    "device = \"cuda:0\" # the device to load the model onto\n",
    "\n",
    "model = AutoModelForCausalLM.from_pretrained(\"/gemini/platform/public/llm/huggingface/Qwen/Qwen1.5-7B-Chat-tp2-pp2-mg2hf\").to(device)\n",
    "tokenizer = AutoTokenizer.from_pretrained(\"/gemini/platform/public/llm/huggingface/Qwen/Qwen1.5-7B-Chat-tp2-pp2-mg2hf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9d3eb4ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.10/dist-packages/transformers/generation/configuration_utils.py:492: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.7` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`.\n",
      "  warnings.warn(\n",
      "/usr/local/lib/python3.10/dist-packages/transformers/generation/configuration_utils.py:497: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.8` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`.\n",
      "  warnings.warn(\n",
      "/usr/local/lib/python3.10/dist-packages/transformers/generation/configuration_utils.py:509: UserWarning: `do_sample` is set to `False`. However, `top_k` is set to `20` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_k`.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sure, here's an implementation of the quicksort algorithm in Python:\n",
      "\n",
      "```python\n",
      "def quicksort(arr):\n",
      "    if len(arr) <= 1:\n",
      "        return arr  # Base case: empty or single-element array is already sorted\n",
      "\n",
      "    pivot = arr[len(arr) // 2]  # Choose a pivot element (can be any, but here we take the middle one)\n",
      "    left = [x for x in arr if x < pivot]  # Elements less than the pivot\n",
      "    middle = [x for x in arr if x == pivot]  # Elements equal to the pivot\n",
      "    right = [x for x in arr if x > pivot]  # Elements greater than the pivot\n",
      "\n",
      "    # Recursively sort the subarrays and concatenate them back together\n",
      "    return quicksort(left) + middle + quicksort(right)\n",
      "\n",
      "# Example usage:\n",
      "unsorted_arr = [3, 6, 8, 10, 1, 2, 1]\n",
      "sorted_arr = quicksort(unsorted_arr)\n",
      "print(sorted_arr)  # Output: [1, 1, 2, 3, 6, 8, 10]\n",
      "```\n",
      "\n",
      "This implementation chooses a pivot element (in this case, the middle one), partitions the array into three parts: elements less than the pivot, elements equal to the pivot, and elements greater than the pivot, and then recursively sorts the left and right subarrays before concatenating them with the middle part.\n"
     ]
    }
   ],
   "source": [
    "prompt = \"Write a quicksort algorithm in python.\"\n",
    "messages = [\n",
    "    {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
    "    {\"role\": \"user\", \"content\": prompt}\n",
    "]\n",
    "text = tokenizer.apply_chat_template(\n",
    "    messages,\n",
    "    tokenize=False,\n",
    "    add_generation_prompt=True\n",
    ")\n",
    "model_inputs = tokenizer([text], return_tensors=\"pt\").to(device)\n",
    "\n",
    "generated_ids = model.generate(\n",
    "    model_inputs.input_ids,\n",
    "    max_new_tokens=512,\n",
    "    do_sample=False,\n",
    ")\n",
    "generated_ids = [\n",
    "    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)\n",
    "]\n",
    "\n",
    "response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b7f213b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Give me a short introduction to large language model.\n"
     ]
    }
   ],
   "source": [
    "text = \"Give me a short introduction to large language model.\"\n",
    "\n",
    "input_ids = tokenizer.encode(text, return_tensors=\"pt\").to(device)\n",
    "\n",
    "output = model.generate(\n",
    "    input_ids, \n",
    "    max_new_tokens=1000,\n",
    "    do_sample=True,\n",
    "    temperature=0.1,\n",
    "    pad_token_id=tokenizer.eos_token_id\n",
    ")\n",
    "\n",
    "# 解码生成的文本\n",
    "generated_text = tokenizer.decode(output[0], skip_special_tokens=True)\n",
    "print(generated_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b36454bc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
